﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Infrastructure.Persistence.AssembliesProvider;
using Common.Infrastructure.Persistence.ConnectionString;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Tool.hbm2ddl;

namespace Common.Infrastructure.Persistence.SessionFactoryBuilding
{
    public class PostgreSQLSessionFactoryBuilder : ISessionFactoryBuilder
    {
        private readonly IConnectionStringProvider _connectionStringProvider;
        private readonly IPersistenceAssemblyProvider _persistenceAssemblyProvider;

        public PostgreSQLSessionFactoryBuilder(IConnectionStringProvider connectionStringProvider, IPersistenceAssemblyProvider persistenceAssembly)
        {
            _connectionStringProvider = connectionStringProvider;
            _persistenceAssemblyProvider = persistenceAssembly;
        }

        public ISessionFactory BuildSessionFactory()
        {

            var fluentConfiguration = Fluently.Configure().Database(
                PostgreSQLConfiguration.PostgreSQL82.ConnectionString(_connectionStringProvider.GetConnectionString()))
                .Mappings(m => m.FluentMappings.AddFromAssembly(_persistenceAssemblyProvider.GetAssembly()));
                                    //.ExposeConfiguration(c => new SchemaExport(c).Create(true, true));

            return fluentConfiguration.BuildSessionFactory();
        }
    }
}
